package com.sy.job;

import com.sy.constant.RedisConstant;
import com.sy.utils.FastDFSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class CleanImgJob {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //每十秒执行一次定时任务
    // 0 */3 * * * ? * :每三分钟执行一次
    // 0 37 20 ? * 7 :每周日的八点37分执行
    @Scheduled(cron = "0/50 * * * * ?") //每50s钟执行一次
    private void task() throws Exception {
        log.info("开始清理垃圾文件....");
        //获取updloadpic - dbpic 的差集，获取到的就是无效数据，要删除的
        Set<String> difference = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_UPLOAD, RedisConstant.SETMEAL_PIC_DB);
        //判断
        if (difference.size() > 0) {
            for (String file : difference) {
                //删除 FastDFS文件服务器中的文件
                FastDFSClient.deleteFile(file);
                //将redis文件名称删除
                redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_UPLOAD,file);
            }
        }

    }

}
